package com.itheima.category.controller;

import com.itheima.category.service.CategoryService;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.pojo.Goods;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wang
 * @description
 * @date 2021/7/30 9:15
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping
    public Result findAll() {
        List<Category> categories =  categoryService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",categories);
    }

    @PostMapping
    public Result add(@RequestBody Category category) {
        boolean add = categoryService.add(category);
        return new Result(add,StatusCode.OK,"添加成功",null);
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage4Get(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ) {
        PageResult pageResult = categoryService.findPage4Get(page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @PostMapping("/search/{page}/{size}")
    public Result findPage4Post(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @RequestBody Category category
    ) {
        PageResult pageResult = categoryService.findPage4Post(page, size,category);
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @GetMapping("/{id}")
    public Result findById(
            @PathVariable("id") Integer id
    ) {
        Category category = categoryService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",category);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(
            @PathVariable("id") Integer id
    ) {
        boolean delete = categoryService.deleteById(id);
        return new Result(delete,StatusCode.OK,"删除成功",null);
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody Category category,
                         @PathVariable("id") Integer id
    ) {
        boolean update = categoryService.updateCategory(category,id);
        return new Result(update,StatusCode.OK,"修改成功",null);
    }
}
